package org.jeecg.modules.parameter.common.biz.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.parameter.common.api.dto.ChangeField;
import org.jeecg.modules.parameter.common.api.dto.PatchPO;
import org.jeecg.modules.parameter.common.biz.service.FzService;
import org.jeecg.modules.parameter.common.utils.oConvertUtils;

import java.util.List;
import java.util.Map;

/**
 * @author P-202207340195
 * @Version: 1.0
 */
@Slf4j
public class FzServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements FzService<T> {

    /**
     * patch更新
     *
     * @param patchPo 要更新的patch
     * @return 执行结果
     */
    @Override
    public boolean updatePatch(PatchPO patchPo) {
        T tEntity = this.getById(patchPo.getId());

        // 解决list类型无法存储的问题
        for (ChangeField field : patchPo.getChangeFields()) {
            if (field.getFieldValue() instanceof List<?>) {
                field.setFieldValue(field.getFieldValue().toString());
            } else if (field.getFieldValue() instanceof Map<?, ?>) {
                field.setFieldValue(field.getFieldValue().toString());
            }
        }

        oConvertUtils.copyPoToEntity(tEntity, patchPo);


        return this.updateById(tEntity);
    }

    /**
     * patch更新
     *
     * @param entity  要更新的entity
     * @param patchPo 要更新的patch
     * @return 执行结果
     */
    @Override
    public boolean updatePatch(T entity, PatchPO patchPo) {
        oConvertUtils.copyPoToEntity(entity, patchPo);
        return this.updateById(entity);
    }

}
